#!/bin/bash

# USAGE:
#        read comments and edit this script
#        delete $0.DOIT within 15 minutes after reboot

# To install and activate this script:
# 1. make sure this script is executable: chmod +x /path/to/this/script.sh
# 2. put in root's crontab: @reboot /path/to/this/script.sh

CHANGED_FILES=
# Edit and un-comment the following line:
# CHANGED_FILES='/etc/dhcp/dhclient.conf /etc/ssh/sshd_config'

# Have $file.proven_good available for each $file in CHANGED_FILES.

# Comment-out these two lines:
/usr/bin/logger "$0 early exit: script not enabled"
exit 1



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#                                                                         # 
#                  Code below does not need editing.                      #
#                                                                         # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

if [ -z $CHANGED_FILES ] ; then
    /usr/bin/logger "$0: \$CHANGED_FILES is empty => early exit"
    exit 1
fi
for f in $CHANGED_FILES; do
    if [ ! -f $f ] ; then
        /usr/bin/logger "$0: missing $f => early exit"
        exit 1
    fi
    if [ ! -f $f.proven_good ] ; then
        /usr/bin/logger "$0: missing $f.proven_good => early exit"
        exit 1
    fi
done

touch "$0.DOIT"
sleep 900                       # time window to remove $0.DOIT by hand
if [ -f "$0.DOIT" ] ; then 
    /usr/bin/logger "$0 rolling back changed files"
    for f in $CHANGED_FILES; do
        mv $f $f.fails
        mv $f.proven_good $f
    done
    /usr/bin/logger "$0 rebooting now"
    shutdown -r now
else
    /usr/bin/logger "$0 not rebooting as $0.DOIT as been removed in time"
fi
